package org.chnxi.study.springcloud.dubbo.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.swing.*;
import java.util.Stack;

@Getter
@Setter
@ToString
public class RpcResponse<T> extends AbstractRpcMessage implements java.io.Serializable {

    /**
     * 服务性能数据信息，响应时需要携带原有的requestTimestamp，并填充responseTimestamp，计算spend
     */
    private PerformanceDescrib performance;

    /**
     * 服务的处理结果
     */
    private RpcStatus status;

    /**
     * 服务处理方信息
     */
    private ApplicationDescrib responder;

    /**
     * 服务处理方返回的业务数据
     */
    private T busiData;

    public static <T,K> RpcResponse<T> buildResponse(RpcRequest<K> request , T busiData, RpcStatus status){
        RpcResponse<T> response = new RpcResponse<>();
        response.setTransaction(TransactionDescrib.newInstance4Response(request.getTransaction()));
        response.setResponder(ApplicationDescrib.getInstance());
        response.setPerformance(PerformanceDescrib.newInstance4Response(request.getPerformance()));
        response.setStatus(status);
        response.setBusiData(busiData);
        return response;
    }

    public static <T,K> RpcResponse<T> buildSuccessDataWithRequest(RpcRequest<K> request, T t){
        return buildResponse(request, t, RpcStatus.SUCCESS);
    }

    public static <T,K> RpcResponse<T> buildErrorDataWithRequest(RpcRequest<K> request , T t){
        return buildResponse(request, t, RpcStatus.ERROR);
    }

}
